<?php
include("general_include.php");
include "checklogin.php";
$page_name="send_item.php";
$IsProcess	= $_REQUEST['IsProcess'];


if($IsProcess!="Y")
	require_once('top.php');
else
	require_once("general_include.php");

$action=$_REQUEST['action'];
$user_id=$_SESSION['user_id'];
//$IsProcess	= 'Y';

	$latest_msg = "SELECT p . *, max(c . message_id )AS order_no  ,DATE_FORMAT(`p`.`date_added` ,'%M  %d, %Y at %h %p' ) AS date_add 
					FROM ".TABLEPREFIX."_message p  LEFT JOIN ".TABLEPREFIX."_message c 
					ON p . message_id =c . reply_of_msg  
					GROUP BY p . message_id   
					HAVING `p` . `reply_of_msg`=0  AND `p` . `user_id_from`=$user_id
					ORDER BY `p`.`message_id`  DESC,order_no DESC";
					
	/*Pagicnation Start */
	$item_per_page_selected=6;
	$PaginationObjAjaxLatest=new PaginationClassAjax($item_per_page_selected,"prev",'',"next","active",$adodbcon);
	$pagination_arr		= $PaginationObjAjaxLatest->PaginationAjax($latest_msg,$page_name."?action=".$action,"ManagerGeneral");	
	/*Pagicnation End */
	
	
	$latest_msgArr = $UserManagerObjAjax->GetRecords("All",$pagination_arr[0]);
	//$latest_msgArr = $UserManagerObjAjax->GetRecords("All",$latest_msg);
	//print_r($latest_msgArr);
	
	for($i=0;$i<count($latest_msgArr);$i++)
	{
	
		$userIsRead = "SELECT * FROM ".TABLEPREFIX."_message 
					WHERE is_read = 'N' AND user_id_from <> $user_id
					AND ( message_id =".$latest_msgArr[$i]['message_id']." OR reply_of_msg=".$latest_msgArr[$i]['message_id'].")";
		$IsRead = $UserManagerObjAjax->GetRecords("All",$userIsRead);
		
		if(count($IsRead)==0)
		{
			 $latest_msgArr[$i]['read'] = 'Y';	
		}	
		else
		{
			$latest_msgArr[$i]['read'] = 'N';
		}
	
		$userImg = "SELECT name,photo FROM ".TABLEPREFIX."_user WHERE user_id='".$latest_msgArr[$i]['user_id_from']."'";
		$ImgArr = $UserManagerObjAjax->GetRecords("Row",$userImg);
					
		$latest_msgArr[$i]['photo'] = $ImgArr['photo'];	
		$latest_msgArr[$i]['name'] = $ImgArr['name'];		
		
		$latest_msg_sub = "SELECT *,DATE_FORMAT(date_added , '%D %M %Y %r' ) AS date_add 
							FROM ".TABLEPREFIX."_message 
							WHERE reply_of_msg=".$latest_msgArr[$i]['message_id']."
							ORDER BY reply_of_msg,message_id ";
		$latest_msg_subArr[$i] = $UserManagerObjAjax->GetRecords("All",$latest_msg_sub);
		if(count($latest_msg_subArr[$i])>0)
		{
			for($j=0;$j<count($latest_msg_subArr[$i]);$j++)
			{
				$userImg2 = "SELECT name,photo FROM ".TABLEPREFIX."_user WHERE user_id='".$latest_msg_subArr[$i][$j]['user_id_from']."'";
				$ImgArr2 = $UserManagerObjAjax->GetRecords("Row",$userImg2);
							
				$latest_msg_subArr[$i][$j]['photo'] = $ImgArr2['photo'];	
				$latest_msg_subArr[$i][$j]['name'] = $ImgArr2['name'];
			}
		}
	}
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('latest_msg_subArr',$latest_msg_subArr);		
$smarty->assign('latest_msgArr',$latest_msgArr);
$smarty->assign('latest_msgArrNum',count($latest_msgArr));
$smarty->assign('IsProcess',$IsProcess);
$smarty->assign('IsProcess',$IsProcess);
$smarty->assign('page_name',$page_name);
$smarty->display("message.tpl");
if($IsProcess!="Y")
	include("footer.php");
/*$latest_msg = "SELECT * FROM ".TABLEPREFIX."_message WHERE user_id_from='".$user_id."' ORDER BY date_added DESC LIMIT 0,10";*/
?>